/** File: 	MyMenuBar.java
 *  Author:	Christopher Hayes
 *	Date:	Oct 15, 2009
 * 	Class:  CMSC 341
 *	Section	0201
 * 
 */
package cx3k.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Schedule;

/**
 * @author chris
 *
 */
public class MyMenuBar extends JMenuBar {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JMenu jmFile, jmHelp, jmSchedule;
	private JMenuItem jmiNew, jmiSave, jmiQuit, jmiLoad, jmiGenerate, jmiNewSchedule, jmiAbout;
	private FileFrame fileframe;
	private Scheduler scheduler;
	private menuListener ML;
	private ArrayList<Integer> scheduleIDs;
	private ArrayList<JMenuItem> scheduleNames;
	private SchedulePane schedulePane;
	private ScheduleListener SL;
	private ProfilePane profilePane;

	/**
	 * 
	 * @param s Scheduler
	 * @param sp SchedulePane
	 * @param profilePane ProfilePane
	 */
	public MyMenuBar(Scheduler s, SchedulePane sp, ProfilePane profilePane)  {
		this.scheduler = s;
		this.schedulePane = sp;
		this.profilePane = profilePane;
		this.ML = new menuListener();
		this.SL = new ScheduleListener();
		jmFile = new JMenu("File");
		jmiNew = new JMenuItem("New");
		jmiNew.addActionListener(ML);
		jmiSave = new JMenuItem("Save");
		jmiSave.addActionListener(ML);
		jmiQuit = new JMenuItem("Quit");
		jmiQuit.addActionListener(ML);
		jmiLoad = new JMenuItem("Load");
		jmiLoad.addActionListener(ML);
		scheduleIDs = new ArrayList<Integer>();
		
		jmHelp = new JMenu("Help");
		jmiAbout = new JMenuItem("About CX3K");
		jmiAbout.addActionListener(ML);
		
		jmSchedule = new JMenu("Schedule");
		
		jmiGenerate = new JMenuItem("Generate");
		jmiGenerate.addActionListener(ML);
		jmiNewSchedule = new JMenuItem("New Schedule");
		jmiNewSchedule.addActionListener(ML);
		
		jmFile.add(jmiNew);
		jmFile.add(jmiLoad);
		jmFile.add(jmiSave);
		jmFile.add(jmiQuit);
		
		
		jmHelp.add(jmiAbout);
		
		this.initalizeJMSchedule();
		
		this.add(jmFile);
		this.add(jmSchedule);
		this.add(jmHelp);
		
		this.fileframe = new FileFrame();
		scheduleNames = new ArrayList<JMenuItem>();
		
	}
	
	/**
	 * Removes all the schedules from the menu.
	 */
	private void initalizeJMSchedule() {
		this.jmSchedule.removeAll();
		this.jmSchedule.addSeparator();
		this.jmSchedule.add(jmiGenerate);
		this.jmSchedule.add(jmiNewSchedule);
	}
	
	/**
	 * Class used to change the schedule within the GUI if multiple schedules were created.
	 * @author Chris
	 *
	 */
	private class ScheduleListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			JMenuItem temp = (JMenuItem) ae.getSource();
			int i = scheduleNames.indexOf(temp);
			schedulePane.loadSchedule(scheduler.getSchedule(scheduleIDs.get(i)));
		}
		
	}
	
	/**
	 * Class used to respond to menu clicks
	 * @author Chris
	 *
	 */
	private class menuListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			
			if (ae.getSource() == jmiAbout) {
				HelpFrame help = new HelpFrame();
			}
			else if(ae.getSource() == jmiNew) {
				scheduler.newProfile("Your name here.");
				profilePane.updateProfile();
				schedulePane.clearSchedule();
				initalizeJMSchedule();
			}
			else if(ae.getSource() == jmiSave) {
				if (profilePane.checkErrors()) {
					String path = fileframe.getSaveLocation();
					if (path != null) scheduler.save(path);
					//scheduler.saveProfile();
				}
			}
			else if(ae.getSource() == jmiQuit) {
				
				System.exit(0);
			}
			else if(ae.getSource() == jmiGenerate) {
				if (profilePane.checkErrors()) {
					//Generate schedule from scheduler
					Schedule s = scheduler.generateSchedule(2005, "Generated");
					
					//Add scheduleID to list of scheduleIDs
					scheduleIDs.add(s.getScheduleId());
					
					//Create new MenuItem and add it to menu
					JMenuItem temp = new JMenuItem(s.getName());
					temp.addActionListener(SL);
					scheduleNames.add(temp);
					jmSchedule.add(temp, 0);
					
					//Display the new schedule 
					schedulePane.loadSchedule(s);
				}
			}
			else if(ae.getSource() == jmiNewSchedule) {
				Schedule s = scheduler.newSchedule("New Schedule");
				//Add scheduleID to list of scheduleIDs
				scheduleIDs.add(s.getScheduleId());
				
				//Create new MenuItem and add it to menu
				JMenuItem temp = new JMenuItem(s.getName());
				temp.addActionListener(SL);
				scheduleNames.add(temp);
				jmSchedule.add(temp, 0);
				
				//Display the new schedule 
				schedulePane.loadSchedule(s);
			}
			else if(ae.getSource() == jmiLoad) {
				//Get path from user
				String path = fileframe.getOpenLocation();
					if(path != null) {
						//Remove all currently loaded schedules
						initalizeJMSchedule();
						scheduleNames = new ArrayList<JMenuItem>();
						scheduleIDs = new ArrayList<Integer>();
						
						//Load the schedules from the file
						int[] IDs = scheduler.loadSchedules(path);
						
						
						//Create all IDs and MenuItems to menu
						JMenuItem temp;
						for (int ID: IDs) {
							if (ID != 0) {
								scheduleIDs.add(ID);
								System.out.println(ID);
								temp = new JMenuItem(scheduler.getSchedule(ID).getName());
								temp.addActionListener(SL);
								scheduleNames.add(temp);
							}
						}
						
						//Display first schedule
						Schedule s = scheduler.getSchedule(IDs[0]);
						if (s != null) schedulePane.loadSchedule(s);
						
						//Add all MenuItems to menu
						for(JMenuItem jmi: scheduleNames) {
							jmSchedule.add(jmi, 0);
						}
						
						//Update the profile with info from file.
						profilePane.updateProfile();
					}
			}
		}
		
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

